//验证
function checkCell(){
	//用户手机号的验证
	$("#cell").on("focus",function(){
		$(this).css("border","1px solid #3FB838")
		$(this).parent().find(".userNum-error").css("display","none");
	});
	$("#cell").on("blur",function(){
		var $userCell = $(this).val();
//		console.log($userCell)
		if ($userCell == "") {
			$(this).parent().find(".userNum-error").css("display","none");
			$(this).css("border","1px solid #cfcfd1")
		} else{
//			console.log(/^[0-9]+$/.test($userCell))
			if (!/^[0-9]+$/.test($userCell)) {
				$(this).parent().find(".userNum-error").css("display","block");
				$(this).css("border","1px solid #de3a39")
				$("#useNum-error-content").html('手机号码格式有误，请输入正确的手机号');
			} else{
				if (!/^1(3|5|7|8|9)\d{9}$/.test($userCell)) {
					$(this).parent().find(".userNum-error").css("display","block");
					$(this).css("border","1px solid #de3a39")
					$("#useNum-error-content").html('请输入正确的手机号');
				} else{
					$(this).parent().find(".ok").css("display","block");
					password($userCell);					
				}
			}
		}
	});
};	
	//验证码的验证
function checkYanZhengMa(){
	var checkNum = {
		img:['../img/yanzhengma0.png','../img/yanzhengma1.png','../img/yanzhengma2.png'],
		num:['n2dk','xth9','61wm']
	};//用对象将数据存起来模拟动态生成验证码
	var $c = 0;
//	console.log(checkNum.img)
	$("#check-num").on("focus",function(){
		$(this).css("border","1px solid #3FB838")
		$(this).parent().find(".userNum-error").css("display","none");
	});
	$("#check-num").on("blur",function(){
		var $userCell = $(this).val();
		$userCell =$userCell.toLocaleLowerCase();//不区分大小写
//		console.log($userCell)
		var checkImg = $(this).parent().find("img").attr("src");
//		console.log(checkImg)
		if ($userCell == "") {
			$(this).parent().find(".userNum-error").css("display","none");
			$(this).css("border","1px solid #cfcfd1")
		}else{
//			console.log($c)
			if($userCell != checkNum.num[$c]){
				$(this).parent().find(".userNum-error").css("display","block");
				$(this).css("border","1px solid #3FB838")
			}else{
				$(this).parent().find(".ok").css("display","block");
				$(this).css("border","1px solid #3FB838");
				return true;
			};
		};
	});
	$(".yanzhengma-text").find("a").on("click",function(){//切换验证码的事件
		if($c>2){
			$c = 0;
		}
		$(this).parent().parent().find('img').attr("src",checkNum.img[$c+1]);
		$c++;
	});
}
function getCellCheckNum(){
	var isGetCell = false;
	$(".getcell").on("click",function(){
		$(this).parent().next().css("display","block");
		$("#get2cellnum").css("background","url(../img/login_bg.png) no-repeat -57px -260px")
		if(isGetCell == true){
			return;
		}
		isGetCell = true;
		var $t = 120;
		var timer = setInterval(function(){
			if($t <= 0){
				isGetCell = false;
				$("#get2cellnum").html("免费获取校验码");
				$("#get2cellnum").css("background","url(../img/login_bg.png) no-repeat -57px -220px")
				clearInterval(timer);
			}
			$("#get2cellnum").html(($t--)+"秒后可重新获取")
		},1000)
	});
	//还有个跟手机联系的接口，返回一个值来验证；
}
function password($userCell){
	$("#password").on("focus",function(){
		$(this).css("border","1px solid #3FB838")
		$(this).parent().find(".userNum-error").css("display","none");
	});
	var $passwords = null;//设一个变量把password的值给引出来
	$("#password").on("blur",function(){
		var $password = $(this).val();
		$passwords = $password;
		if ($password == "") {
			$(this).parent().find(".userNum-error").css("display","none");
			$(this).css("border","1px solid #cfcfd1")
			$("#password-error-content").html('请输入密码');
		} else{
//			console.log(/^[0-9]+$/.test($userCell))
			if ($password.length<6 || $password.length>15) {
				$(this).parent().find(".userNum-error").css("display","block");
				$(this).css("border","1px solid #de3a39")
				$("#password-error-content").html('密码长度只能在6-16位字符之间');
			} else{
				if (!/^[a-zA-Z0-9]+$/.test($password)) {
					$(this).parent().find(".userNum-error").css("display","block");
					$(this).css("border","1px solid #de3a39")
					$("#password-error-content").html('密码过于简单，请以字母数字特殊字符组合');
				} else{
					$(this).parent().find(".ok").css("display","block");
				}
			}
		}
	});
	//重复密码
	$("#password1").on("focus",function(){
		$(this).css("border","1px solid #3FB838")
		$(this).parent().find(".userNum-error").css("display","none");
	});
	$("#password1").on("blur",function(){
		var $password1 = $(this).val();
		if ($password1 == "") {
			$(this).parent().find(".userNum-error").css("display","none");
			$(this).css("border","1px solid #cfcfd1")
			$("#password1-error-content").html('请输入密码');
		}else{
			if ($password1.length<6 || $password1.length>15) {
				$(this).parent().find(".userNum-error").css("display","block");
				$(this).css("border","1px solid #de3a39")
				$("#password1-error-content").html('密码长度只能在6-16位字符之间');
			} else{
//				console.log(password())
				if ($password1 != $passwords) {
					$(this).parent().find(".userNum-error").css("display","block");
					$(this).css("border","1px solid #de3a39")
					$("#password1-error-content").html('两次输入密码不一致');
				} else{
					$(this).parent().find(".ok").css("display","block");
					$("#contract").css("display","inline-block");
					tiJiao($userCell,$password1)
				}
			}
		}
	});
}
function tiJiao($userCell,$password1){
	var isLogin = "isLogin";//判断用户是否登陆
	var myUserName = "myUserName";//用户名所对应的key值
	var myPwd = "myPwd";//密码所对应的key值
	
	$("#tijiao").on("click",function(){
		//判断注册框是否为空
		if ($("#cell").val() == "") {
			$("#cell").parent().find(".userNum-error").css("display","block");
			$("#cell").css("border","1px solid #de3a39")
			$("#useNum-error-content").html('请输入手机号');
		}else{
			if ($("#check-num").val() == "") {
				$("#check-num").parent().find(".userNum-error").css("display","block");
				$("#check-num").css("border","1px solid #de3a39")
			}else{
				if ($("#password").val() == "") {
					$("#password").parent().find(".userNum-error").css("display","block");
					$("#password").css("border","1px solid #de3a39")
					$("#password-error-content").html('请输入密码');
				}else{
					if ($("#password1").val() == "") {
						$("#password1").parent().find(".userNum-error").css("display","none");
						$("#password1").css("border","1px solid #cfcfd1")
						$("#password1-error-content").html('请输入密码');
					}else{
						//给用户的注册信息存cookie里面；
						if(($userCell != "" && $userCell != "undefined")&&($password1 != ""&&$password1 != "undefined")){
							addCookie(myUserName,$userCell,5);
							addCookie(myPwd,$password1,5);
							addCookie(isLogin,0,5);
							window.location = "login.html";
						}
					};
			 	}
			}
		};
	});
}

$(function(){
	$("#register-base").load('lrbase.html #zhuce');
	$("#footer").load("lrbase.html #footer1");
	//手机号码
	checkCell();
	//验证码
	checkYanZhengMa();
	//获取手机验证码
	getCellCheckNum();
	//密码的验证
	password();
	//提交
	tiJiao();
})